I wrote the program on the right to put my algorithm through its paces. Note how I have used two for loops with one inside another. The outer one works through the mask values and the inner one works through the start values.

The trick of keeping track of the biggest number you have found so far and then changing this to reflect a new winner each time is one which you can use whenever you are looking for highest or lowest values, we did something similar with our arrays when we were looking for the highest score.

To keep from getting bored I added a display which lets me view the progress of the program. This is not actually necessary, but it helps you to build confidence that everything is working OK.

Run project Exercise 6.1. Make a note of the result.

If you need to refresh your memory about creating and running project files you should refer back to:
chapter 'Lab 5: LCDs and libraries'
page
'Linkers and libraries'

You can use the random function to obtain a new value from the sequence whenever one is required. If you want a wider range of values you can use a larger register with more bits. Of course you always get the same sequence (which can be useful sometimes) but there are tricks you can play to randomize your position in the sequence. One way is to repeatedly call the function while a button is held down by the user. In real life computers provide randomize functions which use the date and time as 'seeds' to ensure that the same sequence of random numbers is not produced each time.